package com.api.shopapi.mapper;


import com.api.shopapi.entity.Order;
import com.api.shopapi.entity.OrderInfo;
import com.api.shopapi.entity.dto.OrderBusinessPersonalDTO;
import com.api.shopapi.entity.dto.OrderDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Mapper
@Repository
public interface OrderMapper {

    @Insert("insert into `order` " +
            "(" +
            "OrderCode," +
            "UserCode," +
            "Price," +
            "CreateTime," +
            "ReceiveName," +
            "ReceivePhone," +
            "ReceiveArea," +
            "ReceiveAreaPath," +
            "ReceiveAddress," +
            "CashierElderlyCode," +
            "GovernmentCode," +
            "BusinessTopCode" +
            ")" +
            "values" +
            "(" +
            "#{order.OrderCode}," +
            "#{order.UserCode}," +
            "#{order.Price}," +
            "#{order.CreateTime}," +
            "#{order.ReceiveName}," +
            "#{order.ReceivePhone}," +
            "#{order.ReceiveArea}," +
            "#{order.ReceiveAreaPath}," +
            "#{order.ReceiveAddress}," +
            "#{order.CashierElderlyCode}," +
            "#{order.GovernmentCode}," +
            "#{order.BusinessTopCode}" +
            ")")
    public int insert(@Param("order") Order order);

    @Update("update `order` set " +
            "Price = #{order.Price}," +
            "CreateTime=#{order.CreateTime}," +
            "ReceiveName=#{order.ReceiveName}," +
            "ReceivePhone=#{order.ReceivePhone}," +
            "ReceiveArea=#{order.ReceiveArea}," +
            "ReceiveAreaPath=#{order.ReceiveAreaPath}," +
            "ReceiveAddress=#{order.ReceiveAddress}," +
            "GovernmentCode=#{order.GovernmentCode}," +
            "BusinessTopCode=#{order.BusinessTopCode}" +
            " where OrderCode=#{order.OrderCode}")
    public int update(@Param("order") Order order);

    @Delete("delete from `order` where OrderCode=#{orderCode}")
    public int delete(@Param("orderCode") String orderCode);

    @Select("select * from `order` order by CreateTime desc")
    public List<Order> getList();

    @Select("select * from `order` where OrderCode=#{orderCode} limit 1")
    public Order getByOrderCode(@Param("orderCode") String orderCode);

    @Select("select * from `order` where UserCode=#{userCode} and CreateTime >= now()-interval 1 minute")
    public List<Order> getByUserCode(@Param("userCode") String userCode);


    //订单统计
    @Select(
            "<script>" +
                    "select sum(if(ob.Status in (14,16),ob.Price,0)) as TotalMoney," +
                    "sum(if(ob.Status=15,ob.Price,0)) as AbolishMoney , " +
                    "count(if(ob.Status in (14,16),true,null)) as OrderCount," +
                    "count(if(ob.Status=11,true,null)) as NoOrder," +
                    "count(if(ob.Status=12,true,null)) as YesOrder, " +
                    "count(if(ob.Status=13,true,null)) as StartServe," +
                    "count(if(ob.Status in (14,16),true,null)) as SuccessOrder ," +
                    "count(if(ob.Status=16,true,null)) as SchonAppraise," +
                    "count(if(ob.Status=15,true,null)) as TotalAbolish  from `order` as o " +
                    "left join order_business as ob on o.OrderCode=ob.OrderCode " +
                    "left join order_business_appraise as oba on ob.OrderBusinessCode=oba.OrderBusinessCode " +
                    "left join elderly as e on o.UserCode=e.UserCode " +
                    "where o.GovernmentCode=#{governmentCode}" +
                    "<if test =' zoneCode !=\"00000000-0000-0000-0000-000000000000\" '>" +
                    " and e.ZoneCodePath like concat('%',#{zoneCode},'%') " +
                    "</if>" +
                    "</script>"
    )
    List<OrderInfo> getOrderInfo(@Param("governmentCode") String governmentCode,
                                 @Param("zoneCode") String zoneCode);


    //消费用户统计详情
    @Select("select * from `order` as o " +
            "left join `order_business` as ob on o.OrderCode=ob.OrderCode " +
            "left join order_goods as og on ob.OrderBusinessCode=og.OrderBusinessCode" +
            " where  ob.Status in (14,16)  and   UserCode=#{userCode}   ")
    List<OrderDTO> getOrderByUserCode(@Param("userCode") String userCode);


    @Select("select  count(*) from elderly as e left join user as u on e.UserCode=u.UserCode where e.IsCut=0 and StatusAudit=1 and GovernmentCode='fff4f361-f38a-4dc9-8c8a-ce6dbd368d91' ")
    int countElderly();

    @Select("select * " +
            "from `order` as o left join order_business as ob on o.OrderCode=ob.OrderCode " +
            "where o.receiveName = #{receiveName} " +
            "and o.receivePhone = #{receivePhone}" +
            "and ob.CreateTime between #{startTime} and #{endTime}")
    List<OrderBusinessPersonalDTO> getOrderPersonal(@Param("receiveName") String receiveName,
                                                    @Param("receivePhone") String  receivePhone,
                                                    @Param("startTime") String  startTime,
                                                    @Param("endTime") String  endTime);

    @Select("select * from `order` where CashierElderlyCode=#{elderlyCode} and (CreateTime between #{startTime} and #{endTime}) ")
    List<Order> getOrderCreateTime(@Param("elderlyCode") String elderlyCode,
                                   @Param("startTime") Date startTime,
                                   @Param("endTime") Date endTime);


}
